// 标识区实现
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/3/3 16:51
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const encoding = require('../encoding')
var aggregation = require('../common/aggregation')
const {createIdentifierWrapperByBlock} = require("./IdentifierWrapper");
//实现了多继承
var TlvComponentBase = require('./TlvComponentBase').TlvComponentBase

class IdentifierField extends aggregation(TlvComponentBase,encoding.SelfEncodingBase){
    constructor() {
        super();
        this.identifiers = [] // List<Identifier>
    }

    /**
     * @description 添加一个标识
     * @param wrapper
     */
    addIdentifier(wrapper){
        this.identifiers.push(wrapper)
    }

    /**
     * @description 获取标识区中第一个指定类型标识
     * @param tlvType
     * @returns {undefined|*}
     */
    getIdentifierByType(tlvType){
        for(let i = 0; i < this.identifiers.length; i++){
            let identifierWrapper = this.identifiers[i]
            if(identifierWrapper.tlvType === tlvType){
                return identifierWrapper
            }
        }
        // return null
    }

    /**
     * @description 获取指定位置的标识
     * @param index
     * @returns {null|*}
     */
    getIdentifier(index){
        if(index < this.identifiers.length){
            return this.identifiers[index]
        }
        return null
    }

    /**
     * @description 设置指定位置的标识
     * @param index
     * @param wrapper
     */
    setIdentifier(index, wrapper){
        for(let i = this.identifiers.length; i <= index; i++){
            this.addIdentifier({})
        }
        this.identifiers[index] = wrapper
    }

    /**
     * @description 清空标识区所有的标识
     */
    clearIdentifiers(){
        this.identifiers = []
    }

    /**
     * @description 获取所有的标识
     * @returns {[]}
     */
    getIdentifiers(){
        return this.identifiers
    }

    /**
     * @description 将标识区线速编码为 TLV
     * @param enc
     * @returns {number}
     */
    wireEncode(enc){
        if(this.identifiers.length > 0){
            let totalLength = 0
            try{
                // 编码TLV-VALUE
                let tmpLen = 0
                for(let i = this.identifiers.length - 1; i >= 0; i--){
                    if(this.identifiers[i] === {}){
                        continue
                    }
                    tmpLen = this.identifiers[i].wireEncode(enc)
                    totalLength += tmpLen
                }

                // 编码TLV-LENGTH
                tmpLen = enc.prependVarNumber((totalLength))
                // DONE 错误处理
                totalLength += tmpLen

                // 编码 TLV-TYPE
                tmpLen = enc.prependVarNumber((encoding.tlv.FieldType.TlvIdentifierField))
                // DONE 错误处理
                totalLength += tmpLen

                return totalLength
            }catch (e){
                throw e
            }

        }
    }
    toUri(){
        return "need to be implemented"
    }

    /**
     * @description 从 TLV Block 中解码出一个 IdentifierField
     * @param block
     */
    wireDecode(block){
        // 检查 Type 是否正确
        // getType => vlint
        try{
            if(encoding.tlv.expectType(block.getType(), encoding.tlv.FieldType.TlvIdentifierField)){
                // 解析子组件
                block.parseSubElements()
                let identifiers = []
                let elements = block.getSubElements()
                for(let i = 0; i < elements.length; i++){
                    let element = elements[i]
                    let newIdentifierWrapper = createIdentifierWrapperByBlock(element)
                    identifiers.push(newIdentifierWrapper)
                }
                this.identifiers = identifiers
            }
        }catch (e){
            throw e
        }


    }
}

module.exports = {
    IdentifierField:IdentifierField
}
